package com.brainhealth.web.controller.customer;

import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.annotation.RepeatSubmit;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.core.page.TableDataInfo;
import com.brainhealth.customer.domain.ScoreConsumeRecord;
import com.brainhealth.customer.domain.ScoreMallOrder;
import com.brainhealth.customer.domain.ScoreRecord;
import com.brainhealth.customer.domain.params.ScoreRechargeParam;
import com.brainhealth.customer.domain.vo.ScoreBase;
import com.brainhealth.customer.domain.vo.ScoreRechargeOrderVO;
import com.brainhealth.customer.domain.vo.ScoreRecordVO;
import com.brainhealth.customer.domain.vo.ScoreVO;
import com.brainhealth.customer.service.IScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 积分接口
 *
 * @author wll
 */
@RequestMapping("/score/v1")
@RestController
public class ScoreController extends BaseController {

    @Autowired
    IScoreService scoreService;

    @Anonymous
    @RepeatSubmit
    @PostMapping("/create_score_recharge_order")
    public AjaxResult createScoreRechargeOrder(HttpServletRequest request, @RequestBody ScoreRechargeParam scoreRechargeParam) {
        ScoreMallOrder scoreRechargeOrder = scoreService.createScoreRechargeOrder(request, scoreRechargeParam);
        if (null == scoreRechargeOrder) {
            return error("创建积分充值订单异常");
        }
        return success(scoreRechargeOrder);
    }

    @Anonymous
    @PostMapping("/score_recharge_order_pay")
    public AjaxResult scoreRechargeOrderPay(HttpServletRequest request, @RequestBody ScoreRechargeParam scoreRechargeParam) {
        ScoreMallOrder scoreRechargeOrder = scoreService.scoreRechargeOrderPay(request, scoreRechargeParam);
        if (null == scoreRechargeOrder) {
            return error("积分充值异常");
        }
        return success(scoreRechargeOrder);
    }

    @Anonymous
    @GetMapping("/get_score_recharge_order_remain_time")
    public AjaxResult getScoreRechargeOrderRemainTime(HttpServletRequest request) {
        Date date = scoreService.getScoreRechargeOrderRemainTime(request);
        if (null == date) {
            return error("获取积分充值订单支付剩余时间异常");
        }
        return success(date);
    }

    @Anonymous
    @RequestMapping("/score_recharge_order_callback")
    public void scoreRechargeOrderCallback(HttpServletRequest request, HttpServletResponse response) {
        scoreService.scoreRechargeOrderCallback(request, response);
    }

    @Anonymous
    @PostMapping("/get_score_mall_order")
    public AjaxResult getScoreMallOrder(@RequestBody Map<String,String> map) {
        String orderId = map.get("orderId");
        ScoreMallOrder scoreMallOrder = scoreService.getScoreMallOrder(orderId);
        if (null==scoreMallOrder){
            return error();
        }
        return success(scoreMallOrder);
    }

    @Anonymous
    @GetMapping("/get_sore_order_list")
    public TableDataInfo getSoreOrderList(HttpServletRequest request) {
        startPage();
        List<ScoreMallOrder> soreOrderList = scoreService.getSoreOrderList(request);
        List<ScoreRechargeOrderVO> scoreRechargeOrderVOList = scoreService.getScoreRechargeOrderVOList(soreOrderList);
        if (null == soreOrderList || null == scoreRechargeOrderVOList) {
            return null;
        }
        return getDataTableNew(scoreRechargeOrderVOList, soreOrderList);
    }

    @Anonymous
    @GetMapping("/get_sore_order_list2")
    public AjaxResult getSoreOrderList2(HttpServletRequest request) {
        List<ScoreMallOrder> soreOrderList = scoreService.getSoreOrderList(request);
        if (null == soreOrderList) {
            return error("查询用户积分充值订单列表异常");
        }
        return success(soreOrderList);
    }


    @Anonymous
    @GetMapping("/get_score_base")
    public AjaxResult getScoreBase(HttpServletRequest request) {
        ScoreBase score = scoreService.getScoreBase(request);
        if (null == score) {
            return error("查询积分基础信息异常");
        }
        return success(score);
    }

    @Anonymous
    @GetMapping("/get_score")
    public AjaxResult getScore(HttpServletRequest request) {
        ScoreVO score = scoreService.getScore(request);
        if (null == score) {
            return error("查询我的积分异常");
        }
        return success(score);
    }


    @Anonymous
    @GetMapping("/get_score_record")
    public TableDataInfo getScoreRecord(HttpServletRequest request) {
        startPage();
        List<ScoreRecord> scoreRecordList = scoreService.getScoreRecordList(request);
        List<ScoreRecordVO> scoreRecordVOList = scoreService.getScoreRecordVOList(scoreRecordList);
        if (null == scoreRecordList || null == scoreRecordVOList) {
            return null;
        }
        return getDataTableNew(scoreRecordVOList, scoreRecordList);
    }

    @Anonymous
    @GetMapping("/get_score_record2")
    public AjaxResult getScoreRecord2(HttpServletRequest request) {
        List<ScoreRecordVO> scoreRecordVOList = scoreService.getScoreRecord(request);
        if (null == scoreRecordVOList) {
            return error("查询积分获取记录异常");
        }
        return success(scoreRecordVOList);
    }

    @Anonymous
    @GetMapping("/get_score_consume_record")
    public TableDataInfo getScoreConsumeRecord(HttpServletRequest request) {
        startPage();
        List<ScoreConsumeRecord> scoreRecordList = scoreService.getScoreConsumeRecordList(request);
        List<ScoreRecordVO> scoreRecordVOList = scoreService.getScoreConsumeRecord(scoreRecordList);
        if (null == scoreRecordList || null == scoreRecordVOList) {
            return null;
        }
        return getDataTableNew(scoreRecordVOList, scoreRecordList);
    }

    @Anonymous
    @GetMapping("/get_score_consume_record2")
    public AjaxResult getScoreConsumeRecord2(HttpServletRequest request) {
        List<ScoreRecordVO> scoreRecordVOList = scoreService.getScoreConsumeRecord(request);
        if (null == scoreRecordVOList) {
            return error("查询积分消耗记录异常");
        }
        return success(scoreRecordVOList);
    }

    @Anonymous
    @GetMapping("/is_get_new_user_gift")
    public AjaxResult isGetNewUserGift(HttpServletRequest request) {
        Integer result = scoreService.isGetNewUserGift(request);
        if (null == result) {
            return error("查询是否领取新用户见面礼异常");
        }
        return success(result);
    }

    @Anonymous
    @PostMapping("/add_new_user_gift")
    public AjaxResult addNewUserGift(HttpServletRequest request) {
        Integer result = scoreService.addNewUserGift(request);
        if (null == result) {
            return error("领取新用户见面礼异常");
        }
        return success(result);
    }

}
